<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<style type="text/css">
table{
	width: 800px;
	border-collapse: collapse;
}
td,th{
	padding: 8px 15px;
	text-align: center;
}
</style>
	<div id="dialog1" style="display: none;"></div>
	<p>
	<button type="button" onclick="add()">新增</button>
	<button type="button" onclick="search()">查询</button>
	</p>
	<table border="1">
		<thead>
			<th>序号</th>
			<th>教师</th>
			<th>班级</th>
			<th colspan="2">操作</th>
		</thead>
		<tbody id="tbody"></tbody>
	</table>
	
<script type="text/javascript">
var queryParam="";

$(function(){
	$('#dialog1').dialog({
		autoOpen:false,
		title:'编辑',
		modal:true,
		width:400,
		buttons:[{
			text:'提交',
			click : function(){
				var self=$(this);
				var form=$(this).find('form');
				var params=form.serialize();
				var action=form.attr('action');
				if(action=='classmanage/save'){
				$.post(action,params,function(data){
					refreshTable();
					self.dialog('close');
				})				
				}else{
					queryParam=params;
					refreshTable();
					self.dialog('close');
				}
			}
		},{
			text:'退出',
			click:function(){
				$(this).dialog('close');
			}
		}]
	})
	refreshTable();
})


function refreshTable(){
	$.post('classmanage/list',queryParam,function(data){
		var tbody=$('#tbody');
		tbody.empty();
		for(var i=0;i<data.length;i++){
			var bj=data[i];
			var tr=$('<tr/>');
			tr.append($('<td/>').html(bj.id));
			tr.append($('<td/>').html(bj.manager));
			tr.append($('<td/>').html(bj.name));
			tr.append($('<td/>').append($('<button/>').html('修改').attr('onclick','xg('+bj.id+')')));
			tr.append($('<td/>').append($('<button/>').html('删除').attr('onclick','del('+bj.id+')')));
			tbody.append(tr);
		}
		$('button').button();
	})
}
function xg(id){
	var dialog=$('#dialog1');
	dialog.load('classmanage/edit',{id:id},function(){
		dialog.dialog('open');
	})
}
function add(){
	var dialog=$('#dialog1');
	dialog.load('classmanage/edit',function(){
		dialog.dialog('open');
	})
}
function search(){
	var dialog=$('#dialog1');
	dialog.load('classmanage/search',queryParam,function(){
		dialog.dialog('open');
	});
}
function del(id){
	if(confirm('是否要删除这条数据？')){
		$.post('classmanage/delete',{id:id},function(data){
			refreshTable();
		})
	}
}
</script>
